package com.yes_iot.service.impl;

import com.yes_iot.dao.VisualMoudleDao;
import com.yes_iot.dao.VisualMoudleLibraryDao;
import com.yes_iot.entity.VisualDisplay;
import com.yes_iot.entity.VisualMoudleLibrary;
import com.yes_iot.exception.DataValidationException;
import com.yes_iot.service.VisualDisplayService;
import com.yes_iot.dao.VisualDisplayDao;
import lombok.extern.slf4j.Slf4j;
import org.postgresql.util.PSQLException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.UUID;

@Service
public class VisualDisplayServiceImpl extends AbstractEntityService implements VisualDisplayService {

    @Autowired
    private VisualDisplayDao visualDisplayDao;
    @Autowired
    private VisualMoudleDao visualMoudleDao;
    @Autowired
    private VisualMoudleLibraryDao visualMoudleLibraryDao;
    @Override
    public VisualDisplay findByKeyCode(String keycode) {
        return visualDisplayDao.findByKeyCode(keycode);
    }

    @Override
    public List<VisualDisplay> findByParentId(UUID parentId) {
        return visualDisplayDao.findByParentId(parentId);
    }

    @Override
    public VisualDisplay save(VisualDisplay info) {
        try {
            return visualDisplayDao.save(info);
        }catch (Exception t){
            PSQLException e = extractPGSQLConstraintViolationException(t).orElse(null);
            if (e != null && e.getServerErrorMessage() != null && e.getServerErrorMessage().getConstraint() != null
                    && e.getServerErrorMessage().getConstraint().equalsIgnoreCase("visual_display_dev_code_un")) {
                throw new DataValidationException("该设备已有组态无法进行新建！");
            } else {
                throw t;
            }
        }
    }

    @Override
    public List<VisualDisplay> findByDevId(String devCode) {
        return visualDisplayDao.findByDevId(devCode);
    }

    @Override
    public void saveOrUpdate(VisualDisplay obj) {
        visualDisplayDao.save(obj);
    }

    @Override
    @Transactional
    public void delete(UUID id) {
        visualDisplayDao.deleteById(id);
        List<VisualMoudleLibrary> visualMoudelLibraryByVisualDisplayParentId = visualMoudleLibraryDao.findVisualMoudelLibraryByVisualDisplayParentId(id);
        if (visualMoudelLibraryByVisualDisplayParentId!=null&&visualMoudelLibraryByVisualDisplayParentId.size()>0){
            for (VisualMoudleLibrary visualMoudleLibrary:visualMoudelLibraryByVisualDisplayParentId) {
                visualMoudleDao.deleteByVisualMoudleLibraryId(visualMoudleLibrary.getId());
                visualMoudleLibraryDao.delete(visualMoudleLibrary);
            }
        }
    }

    @Override
    public List<VisualDisplay> findByParentIdOrId(UUID id) {
        return visualDisplayDao.findByParentIdOrId(id);
    }

    @Override
    public void updateByKeycode(VisualDisplay obj) {
        visualDisplayDao.updateByKeycode(obj.getKeycode(),obj.getConfig(),obj.getContent(),obj.getLoginText(),obj.getLoginLogo());
    }

    @Override
    public List<VisualDisplay> findByDevModelId(String devModelId) {
        return visualDisplayDao.findByDevModelId(devModelId);
    }

    @Override
    public void deleteByParentId(UUID id) {
        visualDisplayDao.deleteByParentId(id);
    }

    @Override
    public List<VisualDisplay> findByFarmId(String farmId) {
        return visualDisplayDao.findByFarmId(Integer.valueOf(farmId));
    }

    @Override
    public List<VisualDisplay> findByDevCodeList(List<String> devCodeList) {
        return visualDisplayDao.findByDevCodeList(devCodeList);
    }

    @Override
    public List<VisualDisplay> findByFarmIdList(List<Integer> farmIdList) {
        return visualDisplayDao.findByFarmIdList(farmIdList);
    }

    @Override
    public Map<String, Object> findLoginByKeyCode(String keyCode) {
        return visualDisplayDao.findLoginByKeyCode(keyCode);
    }

    @Override
    public VisualDisplay findById(UUID id) {
        return visualDisplayDao.findById(id).get();
    }

    @Override
    public void updateNameAndSeqByKeycode(VisualDisplay obj) {
        visualDisplayDao.updateNameAndSeqByKeycode(obj.getKeycode(),obj.getName(),obj.getSeq());
    }
}
